package com.yypx.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.yypx.common.Constants;
import com.yypx.common.utils.StringUtil;
import com.yypx.entity.Lesson;
import com.yypx.entity.PageBean;
import com.yypx.service.LessonService;

@Scope("prototype")
@Component("lessonAction")
public class LessonAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private LessonService lessonService;
	private List<Map<String, Object>> resultList;
	private PageBean pageBean;
	private static final String PARAM_PAGE = "page";
	private static final String PARAM_PAGESIZE = "pagesize";
	private Map<String, Object> rMap;

	public List<Map<String, Object>> getResultList() {
		return resultList;
	}
 
	public void setResultList(List<Map<String, Object>> resultList) {
		this.resultList = resultList;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public Map<String, Object> getrMap() {
		return rMap;
	}

	public void setrMap(Map<String, Object> rMap) {
		this.rMap = rMap;
	}

	/**
	 * 新增
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		return "add";
	}

	/**
	 * 新增保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addSave() throws Exception {
		Lesson o = new Lesson();
		o.setLname(getRequest().getParameter("lname"));
		o.setLcode(getRequest().getParameter("lcode"));
		o.setLdes(getRequest().getParameter("ldes"));
		o.setCategory(0);
		o.setCreatedby(UUID.randomUUID());
		o.setLid(UUID.randomUUID());
		lessonService.add(o);
		return "addsuccess";
	}

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		Map map = new HashMap<String, Object>();
		int totalCount = lessonService.selCountByCondition(map);
		setPageBean(new PageBean(totalCount, getRequest().getParameter(PARAM_PAGESIZE), getRequest().getParameter(PARAM_PAGE), getRequest().getRequestURI()));
		map.put(Constants.SQL_MAP_LIMIT, pageBean.getPageSize());
		map.put(Constants.SQL_MAP_OFFSET, pageBean.getStartResult());
		List<Lesson> list = lessonService.selAllByCondition(map);
		resultList = lessonService.getResultList(list);
		return "list";
	}

	/**
	 * 浏览
	 * 
	 * @return
	 * @throws Exception
	 */
	public String view() throws Exception {
		UUID lid = null;
		if (StringUtil.isStringHasContent(getRequest().getParameter("lid"))) {
			lid = UUID.fromString(getRequest().getParameter("lid"));
			Lesson o = lessonService.findById(lid);
			if (o != null) {
				setrMap(lessonService.getResultMap(o));
				getRequest().setAttribute("rMap", rMap);
			}
		}
		return "view";
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		UUID lid = null;
		if (StringUtil.isStringHasContent(getRequest().getParameter("lid"))) {
			lid = UUID.fromString(getRequest().getParameter("lid"));
			Lesson o = lessonService.findById(lid);
			if (o != null) {
				setrMap(lessonService.getResultMap(o));
				getRequest().setAttribute("rMap", rMap);
			}
		}
		return "edit";
	}

	/**
	 * 编辑保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editSave() throws Exception {
		Lesson lesson = new Lesson();
		UUID lid = null;
		if (StringUtil.isStringHasContent(getRequest().getParameter("lid"))) {
			lid = UUID.fromString(getRequest().getParameter("lid"));
			lesson = lessonService.findById(lid);
		}
		lesson.setLname(getRequest().getParameter("lname"));
		lesson.setLcode(getRequest().getParameter("lcode"));
		lesson.setLdes(getRequest().getParameter("ldes"));
		lesson.setCategory(0);
		lesson.setLastupdateby(UUID.randomUUID());
		lessonService.update(lesson);
		return "editsuccess";
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		UUID lid = null;
		if (StringUtil.isStringHasContent(getRequest().getParameter("lid"))) {
			lid = UUID.fromString(getRequest().getParameter("lid"));
			try {
				lessonService.delById(lid);
			} catch (Exception e) {
				getRequest().setAttribute("err", e.getMessage());
				return "error";
			}
		}
		return "delete";
	}
}
